<?php
namespace app\controllers;


use app\controllers\common\BaseController;
use app\models\StudentPaper;
use app\models\PaperScoreInfo;
use app\models\TmpStudent;
use app\service\UrlService;
use yii\data\Pagination;
use app\models\Student;
use app\models\Aclass;
use app\models\Teacher;
use yii;
use yii\helpers\BaseJson;

class ClassController extends BaseController
{
    public function actionIndex(){
        //展示所有 班级
        $allclass = Aclass::find()->where(['state'=>1]);
//        var_dump(￥all)
        $count = $allclass->count();
        // 使用总数来创建一个分页对象
        $pagination = new Pagination(['totalCount' => $count]);

        // 使用分页对象来填充 limit 子句并取得文章数据
        $allclass = $allclass->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        return $this->render(UrlService::BuildUrl('class/index'),['data'=>$allclass,'pagination'=>$pagination]);
    }

    public function actionSet(){
        $model = new Aclass();
        if(yii::$app->request->isGet){
            $classid = $this->get('classid',0);
            if($classid){
                $stus = Student::find()->where(['classid'=>$classid])->select(['sid','name','email'])->asArray()->all();
                $tmps = TmpStudent::find()->where(['state'=>0])->select(['student_id','name','email'])->asArray()->all();
                return $this->render(UrlService::BuildUrl('class/setstudent'),['classid'=>$classid,'stus'=>$stus,'tmps'=>$tmps]);
            }
            //选择还没有加入班级的老师
            $headmaster = Teacher::find()->where(['inclass'=>'0'])->select(['id','name'])->asArray()->all();
            $headmaster = array_column($headmaster,'name','id');
            return $this->renderPartial(UrlService::BuildUrl('class/set'),['model'=>$model,'headmaster'=>$headmaster]);
        }
        if( $model->load($this->post(null)) ){
            $res = Teacher::findOne(['id'=>$model->headmasterid]);
            $tname = $res->name;
            $model->headmaster = $tname;
            $model->state = 1;
            if(!$model->save()){
                return $this->render(UrlService::BuildUrl('error/error'),['message'=>'添加失败']);
            }
            return $this->render(UrlService::BuildUrl('error/error'),['message'=>'添成功']);
        }else{
            return $this->render(UrlService::BuildUrl('error/error'),['message'=>'参数错误']);
        }
    }
    public function actionAddstu(){
        $add = $this->post('addstudent',[]);//hr线上面的数据
        $update = $this->post('edCheck',[]);//hr线下面的数据
        $classid = $this->post('classid',0);
        //二话不说，先查询本班的人 colummn是吧获取到的列保存到一个数组内
        $initstudents = Student::find()->where(['classid'=>$classid])->select(['sid'])->asArray()->column();
        //用差集找出要移出班级的人
        $up_res = array_diff($initstudents,$update);
        if(!empty($up_res)){
            Student::deleteAll(['sid'=>$up_res]); //返回删除
            TmpStudent::updateAll(['state'=>0],['student_id'=>$up_res]);//修改临时表状态
        }
        if(!empty($add)){
            TmpStudent::updateAll(['state'=>1],['student_id'=>$add]);//修改临时表状态
            $data = TmpStudent::find()->where(['student_id'=>$add])->select(['student_id','name','email'])->asArray()->all();
            $sid = array_column($data,'student_id');
            $name = array_column($data,'name');
            $email = array_column($data,'email');
            $rukudata = range(0,count($sid)-1,1);

            for($i=0;$i<count($sid);$i++){
                $rukudata[$i] = [];
                array_push($rukudata[$i],$sid[$i]);
                array_push($rukudata[$i],$name[$i]);
                array_push($rukudata[$i],$email[$i]);
                array_push($rukudata[$i],$classid);
            }
            //批量插入数据
            Yii::$app->db->createCommand()->batchInsert(Student::tableName(),
            ['sid','name','email','classid'],$rukudata)->execute();
        }
        return $this->redirect(UrlService::BuildUrl('/class/set',['classid'=>$classid]));
    }


    public function actionScoresurvey(){
        $id = $this->current_user['id'];
        $classid = Teacher::findOne(['teacher'=>$id]);
        if(!$classid){return $this->render('error/error',['message'=>'您的角色不是老师 :(']);}
        $classid = $classid->inclass;
        //获取该班级的成绩
        $classscoreinfo = PaperScoreInfo::find()->where(['classid'=>$classid]);
        $counts = $classscoreinfo->count();//获取数据总条数
        //数据可能比较多，设置分页
        $pagination = new Pagination(['totalCount'=>$counts]);
        $classscoreinfoPage = $classscoreinfo->offset($pagination->offset)->limit($pagination->limit)->all();
        return $this->render(UrlService::BuildUrl('/class/scoresurvey'),[
            'info'=>$classscoreinfoPage,
            'pagination'=>$pagination,
        ]);
    }


}
